﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DONGACS.Common;
using QLPKWeb.BussinessDelegate.CommonDelegate;
using QLPKWeb.BussinessDelegate.PhanQuyen;
using QLPKWeb.Models.LinQ.Common;


namespace QLPKWeb.Controllers
{
    public class CommonController : Controller
    {
        CommonDelegate  CommonDelegate = new CommonDelegate();

        public JsonResult GetDisplayNameByUserIdAjax(string userId)
        {
            try
            {
                string DisplayName = CommonDelegate.GetDisplayNameByUserId(userId);

                return Json(DisplayName, JsonRequestBehavior.AllowGet);
            }
            catch (Exception)
            {
                return Json(string.Empty, JsonRequestBehavior.AllowGet);
            }

        }

       
        public JsonResult GetDisplayNameListByUserIdListAjax(string userIdList)
        {
            try
            {
                IEnumerable<string> userIds = userIdList.Split('|');
                string DisplayNameList = string.Empty;
                TB_User user = new TB_User();

                for (int i = 0; i < userIds.Count() - 1; i++)
                {
                    user = CommonDelegate.GetUserNameByUserId(userIds.ElementAt(i));
                    if (i != userIds.Count() - 2)
                    {
                        DisplayNameList += (string.IsNullOrEmpty(user.UserName) || string.IsNullOrEmpty(user.FirstName)) ? user.UserName + "; " : user.LastName + " " + user.FirstName + "; ";
                    }
                    else
                    {
                        DisplayNameList += (string.IsNullOrEmpty(user.UserName) || string.IsNullOrEmpty(user.FirstName)) ? user.UserName : user.LastName + " " + user.FirstName;
                    }

                }



                return Json(DisplayNameList, JsonRequestBehavior.AllowGet);
            }
            catch (Exception)
            {
                return Json(string.Empty, JsonRequestBehavior.AllowGet);
            }
        }
        
        /// <summary>
        /// Lây tên user trong phòng xử lý và phòng phối hợp thuộc quy trình
        /// </summary>
        /// <auhtor>DucVA</auhtor>
        /// <param name="IdTienTrinh">Khóa của tiến trình </param>
        /// <returns>danh sách tên các user theo định dạng Json</returns>
 
        public JsonResult GetDisplayNemeUserAndDepByUserId(int userID)
        {
            string kq = CommonDelegate.GetDisplayNameAndDepNameByUserId(userID.ToString());
            return Json(kq, JsonRequestBehavior.AllowGet);
        }

        public JsonResult GetDisplayNemeUserAndDepByListUserId(string ListUserID)
        {
            string[] arr = ListUserID.Split(',');
            string listName = string.Empty;
            for (int i = 0; i < arr.Length; i++)
            {
                if (!string.IsNullOrEmpty(arr[i]))
                {
                    listName += CommonDelegate.GetDisplayNameAndDepNameByUserId(arr[i]) + ", ";
                }
            }
            if (!string.IsNullOrEmpty(listName))
                listName = listName.Substring(0, listName.Length - 2);
            return Json(listName, JsonRequestBehavior.AllowGet);
        }
    }
}
